package com.example.springboot.mapper;

import com.example.springboot.domain.Blog;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component(value ="blogMapper")
public interface BlogMapper {

    @Insert("insert into blog (title,content,descr,cover,tags,userId,date) values (#{title},#{content},#{descr},#{cover},#{tags},#{userId},#{date})")
    public int insert(Blog blog);

    @Delete("delete from blog where id=#{id}")
    public int deleteById(@Param("id") Integer id);

    @Update("update blog set title=#{title},content=#{content},descr=#{descr},cover=#{cover},tags=#{tags} where blog.id=#{id}")
    public int updateById(Blog blog);

//    通过blogID查找blog
    @Select("select * from blog where id = #{id}")
    public Blog selectById(@Param("id") Integer id);

    @Select("select * from blog")
    public List<Blog> selectAll(Blog blog);

//    通过用户ID查找blog
    @Select("select * from blog where userId = #{id}")
    public List<Blog> selectUserBlog(@Param("id") Integer id);

//    更新阅读量
    @Update("update blog set readCount = readCount + 1 where id = #{blogId}")
    public void updateReadCount(@Param("blogId") Integer blogId);

//    查找收藏
    @Select("SELECT b.*,u.username FROM collect AS c LEFT JOIN blog b ON c.fid = b.id LEFT JOIN USER u ON u.userid = b.userId WHERE c.userId = #{userId} ORDER BY id DESC")
    public List<Blog> selectCollect(Blog blog);

//    查找点赞
    @Select("SELECT b.*,u.username FROM likes AS l LEFT JOIN blog b ON l.fid = b.id LEFT JOIN USER u ON u.userid = b.userId WHERE l.userId = #{userId} ORDER BY id DESC")
    public List<Blog> selectLike(Blog blog);

}
